<?php
/**
 * Created by PhpStorm.
 * User: 农夫
 * Date: 2017/9/11
 * Time: 9:50
 */
namespace app;

use think\Controller;
use My\DataReturn;
use My\Validate;

abstract class BaseCollection extends Controller
{
    public function __construct()
    {
        parent::__construct();
    }

    //验证登录  首先实始化
    abstract function checkLogin();

    //登录权限验证 首先实始化
    abstract function checkAccess();


    //生成会话session
    public function putSession($data = array(), $prefix = 'admin_')
    {
        if (validate::checkEmpty($data)) DataReturn::returnJson(false, '生成会话必须是数组');
        if (is_array($data)) {
            foreach ($data as $key => $val) {
                session($prefix . $key, $val);
            }
        } else {
            DataReturn::returnJson(false, '生成会话必须是数组');
        }
    }

    //获取上一页URL
    public function previousUrl()
    {
        return $_SERVER['HTTP_REFERER'];
    }

    //读取上一页URL
    public function getPrevious()
    {
        return input('REFERER');
    }

    public function getConfig($string, $value, $file_name = 'config_status')
    {
        $conf_data = require(EXTEND_PATH_CONF . $file_name . '.php');
        return $conf_data[$string][$value];
    }

    /**
     * @param string $msg
     * @param null $url
     * @param string $data
     * @param int $wait
     * @param array $header
     */
    public function returnError($msg = '', $url = null, $data = '', $wait = 3, array $header = [])
    {
        return $this->error($msg, $url, $data, $wait, $header);
    }

    /**
     * 跳转
     * @param $url
     */
    public function toredirect($url)
    {
        header('Location: ' . $url);
        exit;
    }

    /**
     * @param string $msg
     * @param null $url
     * @param string $data
     * @param int $wait
     * @param array $header
     */
    public function returnSuccess($msg = '', $url = null, $data = '', $wait = 3, array $header = [])
    {
        return $this->success($msg, $url, $data, $wait, $header);
    }

    /**
     * 清空会话session
     * @param string $prefix
     * @return bool
     */
    public function deleteSession($prefix = 'admin_')
    {
        $data = $this->getSession();
        foreach ($data as $key => $val) {
            if (strpos($key, $prefix) === 0) {
                session($key, null);
            }
        }
    }

    /**
     * 获取会话内容
     * @param string $string
     * @return mixed
     */
    public function getSession($string = '')
    {
        return session($string);
    }
}